package com.example.springboot_demo.schedule;

import com.example.springboot_demo.controller.FormController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 */
@EnableScheduling
@EnableAsync
//@Component
public class SpringScheduleTask {

    private static final Logger log = LoggerFactory.getLogger(SpringScheduleTask.class);

    @Autowired
    private FormController formController;

    //每5秒执行一次
    @Scheduled(cron = "0/5 * * * * ?")
    @Async
    public void sch1() throws InterruptedException {
        String now = new SimpleDateFormat("yyy-MM-dd HH:mm:ss").format(new Date());
        String name = Thread.currentThread().getName();
        Thread.sleep(2000);
        log.info("sch1:    {} 发送通知，发送时间为：{}", name, now);
        //processor.onApplicationEvent();
        count++;
        if (count > 5) {
            processor.destroy();
        }
    }

    @Autowired
    private ScheduledAnnotationBeanPostProcessor processor;

    int count = 0;

    //每6秒执行一次
    @Scheduled(cron = "0/6 * * * * ?")
    @Async
    public void sch2() {
        String now = new SimpleDateFormat("yyy-MM-dd HH:mm:ss").format(new Date());
        String name = Thread.currentThread().getName();
        log.info("sch2:    {} 发送通知，发送时间为：{}", name, now);
    }
}
